<?php
class Bootstrap 
	extends Zend_Application_Bootstrap_Bootstrap 
{
	
	protected function _initDb ()
    {
        $db = $this->getPluginResource('db')->getDbAdapter();
        $db->query("SET NAMES 'utf8'");
        Zend_Db_Table::setDefaultAdapter($db);
        Zend_Registry::set('db', $db);
    }
	           
	protected function _initConfig() 
	{
	    $config = new Zend_Config($this->getOptions());
	    Zend_Registry::set('configuration', $config);
	}
		
	    	
	protected function _initAutoloaders()
	{ 
		$this->getApplication()->setAutoloaderNamespaces(array('My_'));
		return $this;
	}
	
	protected function _initActionHelpers() 
	{
		Zend_Controller_Action_HelperBroker::addPrefix ( 'My_Controller_Action_Helper' );
		
		$path = dirname(__FILE__) 	. DIRECTORY_SEPARATOR .
				'controllers'		. DIRECTORY_SEPARATOR .
				'helpers';
				
		Zend_Controller_Action_HelperBroker::addPath($path, 'Cron_Action_Helper');
		
		Zend_Controller_Action_HelperBroker::getStaticHelper('BaseInit');
		
	}	
		
	protected function _initModuleAutoloader() 
    {
		$del = new Zend_Application_Module_Autoloader(array(
			'namespace' => 'Delivery' , 
			'basePath' => dirname(dirname(__FILE__)) . '/del'
		));
    	
		$del->addResourceType('collection', 'models/collections', 'Model_Collection');
		
    	$web = new Zend_Application_Module_Autoloader(array(
			'namespace' => 'Web' , 
			'basePath' => dirname(dirname(__FILE__)) . '/web'
		));
    	
    	$adm = new Zend_Application_Module_Autoloader(array(
			'namespace' => 'Adm' , 
			'basePath' => dirname(dirname(__FILE__)) . '/adm'
		));
		
		$cron = new Zend_Application_Module_Autoloader(array(
			'namespace' => 'Cron' , 
			'basePath' => dirname(dirname(__FILE__)) . '/cron'
		));
    	
    	$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
		    'basePath'  => dirname(dirname(dirname(__FILE__))) . '/library/',
		    'namespace' => 'My',
		));
		
	}	

    protected function _initView ()
    {
    	
        // Initialize view
        $view = new Zend_View();
        $view->doctype('XHTML1_STRICT');
//        $view->headTitle('Delivery X Web');
        // Add it to the ViewRenderer
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
        $viewRenderer->setView($view);

        $view->addHelperPath('My/View/Helper', 'My_View_Helper_');
        $view->addHelperPath( dirname(__FILE__) . '/views/helpers', 'Web_View_Helper_');
        $view->addHelperPath( dirname(dirname(__FILE__)) . '/del/views/helpers', 'Del_View_Helper_');
        //ZendX_JQuery::enableView($view);
        // Return it, so that it can be stored by the bootstrap
        return $view;
    }
	
	protected function _initLogger ()
    {
        $log = $this->getPluginResource('log')->getLog();
        
        Zend_Registry::set('Zend_Log', $log);
    }
    
    protected function _initRewrite() {
	    
		$this->bootstrap('FrontController');
		$this->bootstrap('config');
		$front = $this->getResource('FrontController');
		$config = Zend_Registry::get('configuration');
	    $router = $front->getRouter();
	 
	    $language = $config->language;
	    
	    $config = new Zend_Config_Xml(APPLICATION_PATH . "/../web/configs/routes.{$language}.xml");      
	    
	    
	    $router->addConfig($config,'routes');
	    
	    $config = new Zend_Config_Xml(APPLICATION_PATH . "/configs/routes.{$language}.xml");
	    $router->addConfig($config,'routes');
	}
	
	protected function _initTranslate()
	{	
		$lang = $this->getOption('language');
		
		$file1 = implode( DIRECTORY_SEPARATOR, array( dirname(APPLICATION_PATH) , 'del', 'languages', "validate.{$lang}.php"));
		$file2 = implode( DIRECTORY_SEPARATOR, array( dirname(APPLICATION_PATH) , 'del', 'languages', "common.{$lang}.php"));
		$file3 = implode( DIRECTORY_SEPARATOR, array( dirname(APPLICATION_PATH) , 'web', 'languages', "{$lang}.php"));
		$file4 = implode( DIRECTORY_SEPARATOR, array( dirname(APPLICATION_PATH) , 'web', 'languages', "meta-{$lang}.php"));
		
		$translate = new Zend_Translate('array', $file1 , $lang);
		$translate->addTranslation(
			array(
		        'content' => $file2,
		        'locale'  => $lang
		    )
		);	
		
		$translate->addTranslation(
			array(
		        'content' => $file3,
		        'locale'  => $lang
		    )
		);

		$translate->addTranslation(
			array(
		        'content' => $file4,
		        'locale'  => $lang
		    )
		);
		
		Zend_Registry::set('Zend_Translate', $translate);
		Zend_Form::setDefaultTranslator($translate);
		Zend_Controller_Router_Route::setDefaultTranslator($translate);
	}
	
    
}

